package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemInitiatorAssistantBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import androidx.databinding.DataBindingUtil;

/**
 * Created by Administrator on 2018/6/4 0004.
 * 发起人列表adapter
 */

public class InitiatorAdapter extends YfListAdapter<Map<String, Object>> {
    private Context context;
    private String stateData[] = {"待确认","已生效","已完成"};
    private String typeData[] = {"二手房","租赁","一手房","垫资"};

    public InitiatorAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
//        return R.layout.item_initiator_assistant;
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_initiator_assistant, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ItemInitiatorAssistantBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(position);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (map.containsKey("initiateTime")&& null!= map.get("initiateTime"))  //发起时间
            binding.tvTime.setText(sdf.format(new Date(Long.valueOf(map.get("initiateTime").toString()))));
        if(map.containsKey("state")&&null!=map.get("state")){//状态
            binding.tvState.setText(stateData[Integer.parseInt(map.get("state").toString())]);
        }
        if(map.containsKey("name")&&null!=map.get("name")){//发起人名字
            binding.initatorName.setText(map.get("name").toString());
        }
        if(map.containsKey("type")&&null!=map.get("type")){//类型
            binding.tvType.setText(typeData[Integer.parseInt(map.get("type").toString())]);
        }
        if(map.containsKey("collaboratorname")&&null!=map.get("collaboratorname")){//协助人名字
            JSONArray jsonArray = JSON.parseArray(map.get("collaboratorname").toString());
            String str = "";
            for (int i = 0;i<jsonArray.size();i++){
                if(i==jsonArray.size()-1){
                    str = str+jsonArray.get(i).toString();
                }else{
                    str = str+jsonArray.get(i).toString()+"、";
                }
            }
            binding.assistantName.setText(str);
        }
        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = (TextView) itemView.findViewById(R.id.txt);
            progress = (AVLoadingIndicatorView) itemView.findViewById(R.id.progress);
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }

}
